<?php
class Pageley_Commerce_CustomerTests extends Pageley_Commerce_DomainTests {

	public function setUp() {
		parent::setUp();
	    $factory = Pageley_Controller_Session_Factory::getInstance();
        $session = $factory->getSession();
        $session->remove("commerce.customer");
	}
	
    public function tearDown() {
        parent::tearDown();
        $this->dispatchRequest(array(
            "controller" => "Query",
            "typeName" => "commerce.Customer",
            "propertyNames" => array("username"),
            "operators" => array("!="),
            "values" => array("bill.gates")
        ));
        $customers = $_REQUEST["entities"];
        foreach($customers as $customer) {
            $this->dispatchRequest(array(
                "controller" => "Delete",
                "typeName" => "commerce.Customer",
                "id" => $customer->getId()
            ));
        }
    }

    public function testLogInCustomer() {
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/LogInCustomer",
            "username" => "bill.gates",
            "password" => "irock"
        ));
        $this->assertTrue($success);
        $factory = Pageley_Controller_Session_Factory::getInstance();
        $session = $factory->getSession();
        $customer = $session->get("commerce.customer");
        $this->assertNotNull($customer);
        $this->assertEquals("Bill Gates", $customer->get("contactName"));
    }
    
    public function testLogOutCustomer() {
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/LogInCustomer",
            "username" => "bill.gates",
            "password" => "irock"
        ));
        $this->assertTrue($success);
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/LogOutCustomer"
        ));
        $this->assertTrue($success);
        $factory = Pageley_Controller_Session_Factory::getInstance();
        $session = $factory->getSession();
        $customer = $session->get("commerce.customer");
        $this->assertNull($customer);
    }
    
    public function testRegisterCustomer() {
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/RegisterCustomer",
            "contactName" => "Steve Jobs",
            "username" => "steve.jobs",
            "password" => "irocktoo",
            "repeatPassword" => "irocktoo",
            "address.line1" => "Macstreet 1",
            "address.postalCode" => "1",
            "address.city" => "Appletown",
            "emailAddress" => "steve.jobs@apple.com",
            "phoneNumber" => "+1 500 333 4444",
            "companyName" => "Apple",
            "vatCode" => "EZ00000000001"
        ));
        $this->assertTrue($success);
        $success = $this->dispatchRequest(array(
            "controller" => "Query",
            "typeName" => "commerce.Customer",
            "propertyNames" => array("username"),
            "operators" => array("="),
            "values" => array("steve.jobs")
        ));
        $customers = $_REQUEST["entities"];
        $this->assertEquals(1, $customers->size());
        $customer = $customers->get(0);
        $this->assertNotNull($customer);
        $this->assertEquals("Steve Jobs", $customer->get("contactName"));
        $this->assertEquals("Appletown", $customer->get("address.city"));
        $this->assertEquals(Pageley_Commerce_Domain_CustomerStatus::$ACTIVE, $customer->get("status"));
    }
    
    public function testListOrdersByCustomer() {
        $success = $this->dispatchRequest(array(
            "controller" => "Commerce/LogInCustomer",
            "username" => "bill.gates",
            "password" => "irock"
        ));
        $this->assertTrue($success);
        $factory = Pageley_Controller_Session_Factory::getInstance();
        $session = $factory->getSession();
        $customer = $session->get("commerce.customer");
    }

}